package com.example.com.common.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * activity 栈管理

 * 
 */
public class ActivityTack {

	public List<Activity> activityList = new ArrayList<Activity>();

	public static ActivityTack tack = new ActivityTack();

	public static ActivityTack getInstanse() {
		return tack;
	}

	private ActivityTack() {

	}

	public void addActivity(Activity activity) {
		activityList.add(activity);
	}

	public void removeActivity(Activity activity) {
		activityList.remove(activity);
	}

	/**
	 * 结束所有activity
	 * 
	 */
	public void finishAll() {
		try {
			for (int i = activityList.size() - 1; i >= 0; i--) {
				Activity activity = activityList.remove(i);
				activity.finish();
			}
		} catch (Exception e) {
			System.exit(0);
		}
	}

	/**
	 * 根据class name获取activity
	 * 
	 * @param name
	 * @return
	 */
	public Activity getActivityByClassName(String name) {
		for (Activity ac : activityList) {
			if (ac.getClass().getName().indexOf(name) >= 0) {
				return ac;
			}
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public Activity getActivityByClass(Class cs) {
		for (Activity ac : activityList) {
			if (ac.getClass().equals(cs)) {
				return ac;
			}
		}
		return null;
	}

	/**
	 * 弹出activity
	 * 
	 * @param activity
	 */
	public void popActivity(Activity activity) {
		removeActivity(activity);
		activity.finish();
	}

	/**
	 * 弹出activity到
	 * 
	 * @param cs
	 */
	@SuppressWarnings("rawtypes")
	public void popUntilActivity(Class... cs) {
		List<Activity> list = new ArrayList<Activity>();
		for (int i = activityList.size() - 1; i >= 0; i--) {
			Activity ac = activityList.get(i);
			boolean isTop = false;
			for (Class element : cs) {
				if (ac.getClass().equals(element)) {
					isTop = true;
					break;
				}
			}
			if (!isTop) {
				list.add(ac);
			} else {
				break;
			}
		}
		for (Activity activity : list) {
			popActivity(activity);
		}
	}
	//结束掉所有该类的实例
	public void finishAllActivity(Class clazz){
		for (int i = activityList.size() - 1; i >= 0; i--) {
			Activity activity = activityList.get(i);
			if(activity.getClass().equals(clazz)) {
				activity.finish();
			}
		}
	}
	//结束掉所有该类及子类的实例
	public void finishAllSubActivity(Class clazz){
		for (int i = activityList.size() - 1; i >= 0; i--) {
			Activity activity = activityList.get(i);
			if(clazz.isInstance(activity)){
				activity.finish();
			}
		}
	}
	//除了某个对象，全都结束
	public void finishAllBut(Activity butObj) {
		try {
			for (int i = activityList.size() - 1; i >= 0; i--) {
				Activity activity = activityList.get(i);
				if(butObj != activity) {
					activity.finish();
					activityList.remove(i);
				}
			}
		} catch (Exception e) {

		}
	}

	//除了某个类的对象，全都结束
	public void finishAllBut(Class butClazz) {
		try {
			for (int i = activityList.size() - 1; i >= 0; i--) {
				Activity activity = activityList.get(i);
				if(!activity.getClass().equals(butClazz)) {
					activity.finish();
					activityList.remove(i);
				}
			}
		} catch (Exception e) {

		}
	}
}
